package ee.ttu.agent.smartHouse.agent;

import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import ee.ttu.agent.smartHouse.gui.TestAgentUI;

public class TestAgent extends Agent {
	private static final long serialVersionUID = 1509531441244210271L;

	private class MatchXOntology implements MessageTemplate.MatchExpression {
		private static final long serialVersionUID = -5483863355032680507L;

		public boolean match(ACLMessage msg) {
//			String ontology = msg.getOntology();
//			return (ontology != null && ontology.startsWith("X"));
			return true;
		}
	} // END of inner class MatchXOntology

	private MessageTemplate template = MessageTemplate.and(
			MessageTemplate.MatchPerformative(ACLMessage.REQUEST),
			new MessageTemplate(new MatchXOntology()));

	protected void setup() {
		final TestAgentUI g = new TestAgentUI();
		g.setVisible(true);
		System.out.println("Agent " + getLocalName() + " is ready.");

		addBehaviour(new CyclicBehaviour(this) {
			private static final long serialVersionUID = -6409202646449209616L;

			public void action() {
				ACLMessage msg = myAgent.receive(template);
				if (msg != null) {
					System.out.println("Message matching custom template received:");
					System.out.println(msg);
					g.showMsg(msg.getContent());
				} else {
					block();
				}
			}
		});
	}
}
